package cn.modfun.web.core.plugin.quartz;

import java.util.List;
import java.util.Properties;

import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.IPlugin;

import cn.hutool.setting.Setting;
import cn.hutool.setting.dialect.Props;

public class QuartzPlugin implements IPlugin {

	private Logger logger = LoggerFactory.getLogger(getClass());
	public static boolean dsAlone = false;
	/**
	 * 默认配置文件*
	 */
	private String config_file_path = "config/quartz.txt";

	private String jobs_file_path = "config/quartz_jobs.txt";

	public QuartzPlugin() {

	}

	public String getConfigFilePath() {
		return config_file_path;
	}

	public void setConfigFilePath(String config_file_path) {
		this.config_file_path = config_file_path;
	}

	public String getJobsFilePath() {
		return jobs_file_path;
	}

	public void setJobsFilePath(String jobs_file_path) {
		this.jobs_file_path = jobs_file_path;
	}
	
	public QuartzPlugin(String jobs_file_path) {
		this.jobs_file_path = jobs_file_path;
	}

	public QuartzPlugin(String config_file_path, String jobs_file_path) {
		this.config_file_path = config_file_path;
		this.jobs_file_path = jobs_file_path;
	}

	public boolean start() {
		try {
			// 实例化
			cn.modfun.web.core.plugin.quartz.QuartzKit.setSchedulerFactory(new StdSchedulerFactory(new Props(config_file_path)));
			// 获取Scheduler
			Scheduler sched = cn.modfun.web.core.plugin.quartz.QuartzKit.getSchedulerFactory().getScheduler();
			// 内存,数据库的任务
			sched.start();
			// 属性文件中的任务
			startPropertiesJobs();
			return true;
		} catch (Exception e) {
			throw new RuntimeException("Can't start quartz plugin.", e);
		}
	}

	public boolean stop() {
		try {
			cn.modfun.web.core.plugin.quartz.QuartzKit.getSchedulerFactory().getScheduler().shutdown();
			cn.modfun.web.core.plugin.quartz.QuartzKit.setSchedulerFactory(null);
			return true;
		} catch (Exception e) {
			throw new RuntimeException("Can't stop quartz plugin.", e);
		}
	}

	public void startPropertiesJobs() {
		Setting config = new Setting(jobs_file_path);
		List<String> jobs = config.getGroups();
		if (!jobs.isEmpty()) {
			for (int i = 0, size = jobs.size(); i < size; i++) {
				Properties job = config.getProperties(jobs.get(i));
				/* 判断任务是否启用 */
				if (!Boolean.valueOf(job.getProperty("enable"))) {
					continue;
				}
				cn.modfun.web.core.plugin.quartz.QuartzKey quartzKey = new cn.modfun.web.core.plugin.quartz.QuartzKey(
						StrKit.isBlank(job.getProperty("id")) ? i + "" : job.getProperty("id"),
						StrKit.isBlank(job.getProperty("name")) ? jobs.get(i) : job.getProperty("name"),
						job.getProperty("group"));
				cn.modfun.web.core.plugin.quartz.job.QuartzJob quartzJob = cn.modfun.web.core.plugin.quartz.QuartzKit.getJob(quartzKey);
				if (quartzJob != null) {
					logger.info("This  job  has started," + quartzKey);
					continue;
				}

				String cron = job.getProperty("cron");
				String className = job.getProperty("class");
				Class clazz;
				try {
					clazz = Class.forName(className);
				} catch (ClassNotFoundException e) {
					throw new RuntimeException(e);
				}
				// 启动任务
				new cn.modfun.web.core.plugin.quartz.job.QuartzCronJob(quartzKey, cron, clazz).start();
			}
		}
	}

}
